# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2024/3/31
desc: 
sample: 
"""
import csv
import os

import allure
import pytest
import requests

from utils import getcsvlist, getcsvDict, getExcelList, getExcelDict


def setup_function(function):
    print("Function setup")
    # 准备函数级资源

def teardown_function(function):
    print("Function teardown")
    # 清理函数级资源

@allure.parent_suite('我是parent_suite')
@allure.suite('我是suite')
@allure.sub_suite('我是sub_suite')
@allure.feature('测试参数化')
@pytest.mark.parametrize("username",['admin','张三','admin'])
def test_parametrize1(username):
    print(username)

@pytest.mark.parametrize("username",[('admin','123456'),('test1','123')])
def test_parametrize2(username):
    print(username)


test_data = [
    {
        'case': '登入成功',
        'usr': 'admin',  # 正常登入
        'psw': '123456'
    },
    {
        'case': '账号不存在',
        'usr': 'admin1',  # 账号不存在
        'psw': '123456'
    },
    {
        'case': '密码错误',
        'usr': 'admin',  # 密码错误
        'psw': '12345'
    },
    {
        'case': '账号或密码为空',
        'usr': '',  # 账号或密码为空
        'psw': ''
    },
]

@pytest.mark.parametrize('param', test_data,ids=[item.get('case') for item in test_data])
def test_parametrize3(param):
    usr = param.get('usr')
    psw = param.get('psw')
    print(f'usr: {usr} , psw: {psw}')


host = 'http://121.11.97.201:9088/'
@pytest.mark.parametrize("id",[1,2,3,4,5])
def test_get(id):
    url =host+'pinter/com/getSku11212?id='+str(id)
    resp = requests.get(url=url)
    assert resp.status_code == 200



#数据结构封装
host = 'http://121.11.97.201:9088/'
# base_path = os.path.dirname(__file__)
# csvfile = os.path.join(base_path, 'data', 'id.csv')
# with open(csvfile, "r",encoding="utf-8") as source_file:
#     reader = csv.reader(source_file)
#     next(reader)
#     mydata = list(reader)

@pytest.mark.flaky(reruns=3,reruns_delay=2)
@pytest.mark.parametrize("id,message",getcsvlist('id.csv'))
def test_get2(id,message):
    url =host+'pinter/com/getSku?id='+str(id)
    resp = requests.get(url=url)
    assert resp.status_code == 200
    assert resp.json().get('message') == message


host = 'http://121.11.97.201:9088/'
csvlist =getcsvDict('id2.csv')
@pytest.mark.parametrize("parmas",csvlist,ids=[data.get('desc') for data in csvlist])
@pytest.mark.flaky(reruns=3,reruns_delay=2)
def test_get3(parmas):
    id=parmas.get('id')
    url =f'{host}pinter/com/getSku?id={id}'
    resp = requests.get(url=url)
    assert resp.status_code == 200
    assert resp.json().get('message') == parmas.get('message')



@pytest.mark.parametrize("case,usr,pwd",getExcelList('case.xlsx'))
@pytest.mark.flaky(reruns=3,reruns_delay=2)
def test_post(case,usr,pwd):
    url =host+'pinter/com/login'
    #userName=admin&password=1234
    data={'userName':'admin','password':'1234'}
    data['userName']=usr
    data['pwd']=pwd
    res = requests.post(url=url,data=data)
    assert res.status_code == 200

excelDict=getExcelDict('case.xlsx')
@pytest.mark.parametrize("parmas",excelDict,ids=[data.get("case") for data in excelDict])
@pytest.mark.flaky(reruns=3,reruns_delay=2)
def test_post(parmas):
    url =host+'pinter/com/login'
    #userName=admin&password=1234
    data={'userName':'admin','password':'1234'}
    data['userName']=parmas.get('usr')
    data['pwd']=parmas.get('pwd')
    res = requests.post(url=url,data=data)
    assert res.status_code == 200


data_1 = [x for x in range(10)]
data_2 = [x for x in range(100)]
@pytest.mark.parametrize('a', data_1)
@pytest.mark.parametrize('b', data_2)
def test_parametrize_1(a, b):
 #sleep(0.01)
 print(f'笛卡尔积 测试数据为 ： {a}，{b}')

#main方法无效
if __name__ == '__main__':
    pass
    #test_login2()